﻿Ext.define('app.system.companylevel.Index',
{
    extend: 'Ext.panel.Panel',
    border: true,
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.items = [
            me.getCompanyTree(),
            me.getUserList()
        ];
        me.callParent(arguments);
    },
    getCompanyTree: function () {
        var me = this;
        if (!this.companyTree) {
            this.companyTree = new Ext.tree.Panel({
                border: false,
                split: true,
                region: 'west',
                title: '科室管理',
                width: 500,
                tbar: [
                    "->",
                    {
                        text: '新增事业部', icon: '/Content/images/add.png', handler: function () {
                            var win = Ext.create("app.system.business.Edit", { title: '新增事业部' });
                            win.on("savesuccess", function () {
                                me.companyTree.store.load({
                                    callback: function () {
                                        me.companyTree.expandAll();
                                    }
                                });
                                win.close();
                            });
                            win.show();
                        }
                    }
                ],
                store: new Ext.data.TreeStore({
                    root: { expanded: true },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/business/GetCompanyLevel'
                    }
                }),
                columns: [{
                    xtype: 'treecolumn',
                    header: '名称',
                    width: 200,
                    sortable: false,
                    dataIndex: 'text'
                },
                {
                    header: '负责人',
                    sortable: false,
                    dataIndex: 'principal'
                }],
                root: { id: '0', text: '' },
                useArrows: true,
                rootVisible: false,
                listeners: {
                    itemclick: function (tree, record, item, index, e, eOpts) {
                        me.getUserList().store.filter = {
                            BusinessId: record.data.businessId,
                            RdCenterId: record.data.rdCenterId,
                            LaboratoryId: record.data.laboratoryId,
                            CompanyType: record.data.companyType
                        };
                        me.getUserList().store.load();
                    },
                    afterrender: function (tree) { tree.expandAll(); },
                    itemcontextmenu: function (menutree, record, item, index, e, eOpts) {
                        e.preventDefault();
                        e.stopEvent();
                        var nodemenu = new Ext.menu.Menu({
                            floating: true,
                            items: [
                            {
                                icon: '/Content/images/add.png',
                                text: "新增研发中心",
                                hidden: record.data.companyType > 1,
                                handler: function () {
                                    var win = Ext.create("app.system.rdcenter.Edit", { title: '新增研发中心', businessId: record.data.businessId });
                                    win.on("savesuccess", function () {
                                        menutree.store.load({
                                            callback: function () {
                                                me.companyTree.expandAll();
                                            }
                                        });
                                        win.close();
                                    });
                                    win.show();
                                }
                            },
                            {
                                icon: '/Content/images/add.png',
                                text: "新增研究室",
                                hidden: record.data.companyType == 1 || record.data.companyType == 3,
                                handler: function () {
                                    var win = Ext.create("app.system.laboratory.Edit", { rdCenterId: record.data.rdCenterId, title: '新增研究室' });
                                    win.on("savesuccess", function () {
                                        menutree.store.load({
                                            callback: function () {
                                                me.companyTree.expandAll();
                                            }
                                        });
                                        win.close();
                                    });
                                    win.show();
                                }
                            },
                            {
                                icon: '/Content/images/edit.gif',
                                text: "编辑事业部",
                                hidden: record.data.companyType != 1,
                                handler: function () {
                                    var win = Ext.create("app.system.business.Edit", { title: '编辑事业部', businessId: record.data.businessId });
                                    win.on("savesuccess", function () {
                                        menutree.store.load({
                                            callback: function () {
                                                me.companyTree.expandAll();
                                            }
                                        });
                                        win.close();
                                    });
                                    win.show();
                                }
                            },
                            {
                                icon: '/Content/images/delete.gif',
                                text: "删除事业部",
                                hidden: record.data.companyType != 1,
                                handler: function () {
                                    Ext.Msg.confirm('系统提示', '确定要删除事业部吗？', function (btn) {
                                        if (btn == 'yes') {
                                            App.AjaxDelete('/api/services/app/business/Delete?businessId=' + record.data.businessId, function () {
                                                menutree.store.load({
                                                    callback: function () {
                                                        me.companyTree.expandAll();
                                                    }
                                                });
                                            });
                                        }
                                    });
                                }
                            },
                            {
                                icon: '/Content/images/edit.gif',
                                text: "编辑研发中心",
                                hidden: record.data.companyType != 2,
                                handler: function () {
                                    var win = Ext.create("app.system.rdcenter.Edit", { title: '编辑研发中心', rdCenterId: record.data.rdCenterId });
                                    win.on("savesuccess", function () {
                                        menutree.store.load({
                                            callback: function () {
                                                me.companyTree.expandAll();
                                            }
                                        });
                                        win.close();
                                    });
                                    win.show();
                                }
                            },
                            {
                                icon: '/Content/images/delete.gif',
                                text: "删除研发中心",
                                hidden: record.data.companyType != 2,
                                handler: function () {
                                    Ext.Msg.confirm('系统提示', '确定要删除研发中心吗？', function (btn) {
                                        if (btn == 'yes') {
                                            App.AjaxDelete('/api/services/app/rdCenter/Delete?rdCenterId=' + record.data.rdCenterId, function () {
                                                menutree.store.load({
                                                    callback: function () {
                                                        me.companyTree.expandAll();
                                                    }
                                                });
                                            });
                                        }
                                    });
                                }
                            },
                            {
                                icon: '/Content/images/edit.gif',
                                text: "编辑研究室",
                                hidden: record.data.companyType != 3,
                                handler: function () {
                                    var win = Ext.create("app.system.laboratory.Edit", { rdCenterId: record.data.rdCenterId, laboratoryId: record.data.laboratoryId, title: '编辑研究室' });
                                    win.on("savesuccess", function () {
                                        menutree.store.load({
                                            callback: function () {
                                                me.companyTree.expandAll();
                                            }
                                        });
                                        win.close();
                                    });
                                    win.show();
                                }
                            },
                            {
                                icon: '/Content/images/delete.gif',
                                text: "删除研究室",
                                hidden: record.data.companyType != 3,
                                handler: function () {
                                    Ext.Msg.confirm('系统提示', '确定要删除研究室吗？', function (btn) {
                                        if (btn == 'yes') {
                                            App.AjaxDelete('/api/services/app/laboratory/Delete?laboratoryId=' + record.data.laboratoryId, function () {
                                                menutree.store.load({
                                                    callback: function () {
                                                        me.companyTree.expandAll();
                                                    }
                                                });
                                            });
                                        }
                                    });
                                }
                            }]
                        });
                        nodemenu.showAt(e.getXY());
                    }
                }
            });
        }
        return this.companyTree;
    },
    getUserList: function () {
        var me = this;
        if (!this.userList) {
            this.userList = new Ext.grid.Panel({
                title: '科室用户',
                region: 'center',
                tbar: [
                    "->",
                    {
                        text: '新增成员', icon: '/Content/images/add.png', handler: function () {
                            var record = me.companyTree.getSelection()[0];
                            if (!record) {
                                Ext.Msg.alert("系统提示", "请选择一个科室");
                                return false;
                            }
                            var win = Ext.create("app.system.companylevel.EditMember", {
                                title: '选择科室成员',
                                BusinessId: record.data.businessId,
                                RdCenterId: record.data.rdCenterId,
                                LaboratoryId: record.data.laboratoryId,
                                CompanyType: record.data.companyType
                            });
                            win.on("savesuccess", function () {
                                me.userList.store.load();
                            });
                            win.show();
                        }
                    }
                ],
                viewConfig: {
                    enableTextSelection: true //可以复制单元格文字
                },
                flex: 2,
                multiColumnSort: true,
                forceFit: true,
                store: Ext.create("app.ux.PrmStore", { url: '/api/services/app/business/GetCompanyUser', autoLoad: false }),
                columns: [
                    { header: '账号', dataIndex: 'userName' },
                    { header: '姓名', dataIndex: 'name' },
                    {
                        xtype: 'actioncolumn',
                        width: 35,
                        header: localize('Operating'),
                        items: [
                        {
                            icon: '/Content/images/delete.gif',
                            tooltip: '移除',
                            handler: function (grid, rowIndex, colIndex) {
                                var record = grid.store.getAt(rowIndex);

                                var selected = me.companyTree.getSelection()[0];
                                if (!selected) {
                                    Ext.Msg.alert("系统提示", "请选择一个科室");
                                    return false;
                                }

                                Ext.Msg.confirm('系统提示', '确定要移除科室用户吗？',
                                  function (btn) {
                                      if (btn == 'yes') {
                                          App.AjaxRemove('/api/services/app/business/RemoveCompanyUser', {
                                              UserId: record.data.id,
                                              BusinessId: selected.data.businessId,
                                              RdCenterId: selected.data.rdCenterId,
                                              LaboratoryId: selected.data.laboratoryId,
                                              CompanyType: selected.data.companyType
                                          }, function (data) {
                                              grid.store.removeAt(rowIndex);
                                          });
                                      }
                                  });
                            }
                        }
                        ]
                    }
                ]
            });
        }
        return this.userList;
    }
});